//
//  GGToastView.m
//  GGToast
//
//  Created by 好米有易 on 2017/10/25.
//  Copyright © 2017年 好米有易. All rights reserved.
//

#import "GGToastView.h"

#define SCREEN_WDITH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define TOAST_WDITH 260

#define TOAST_HEIGHT 40

#define DEFAULT_X (SCREEN_WDITH - TOAST_WDITH) / 2

#define DEFAULT_Y (SCREEN_HEIGHT - TOAST_HEIGHT) / 2

#define CORNERRADIUS 5

#define DISMISS_TIME 1.0

@interface GGToastView()

/** 提示文字 */
@property (nonatomic, strong) UILabel *tipsLabel;

@property (nonatomic, strong) UIActivityIndicatorView * indecator;

@end

@implementation GGToastView


+ (GGToastView *)sharedToastView {
    static GGToastView *sharedToastView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^
    {
        sharedToastView = [[GGToastView alloc] init];
    });
    return sharedToastView;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    
    
    if (self) {
    
      
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.8];
        [self setupUI];
    }
    
    return self;
}


+ (void)showToastWithTitle:(NSString *)title {
    
    GGToastView *toastView = [self createToastViewWithTile:title];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:toastView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DISMISS_TIME * 2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
       
            [toastView dismissToastView:toastView];
   
    });
   
}


- (void)dismissToastView:(GGToastView *)toastView {
    
    [UIView animateWithDuration:DISMISS_TIME  animations:^{
        
        toastView.alpha = 0;
        
    } completion:^(BOOL finished) {
        
        [toastView removeFromSuperview];
        toastView.alpha = 1;
        
    }];
    
}

+ (void)dissmissLoadingView {
    
    [UIView animateWithDuration:DISMISS_TIME  animations:^{
        
        [GGToastView sharedToastView].alpha = 0;
        
    } completion:^(BOOL finished) {
        [[GGToastView sharedToastView] stopLoading];
        [[GGToastView sharedToastView] removeFromSuperview];
        [GGToastView sharedToastView].alpha = 1;
        
    }];
}


+ (void)showLoadingToastWithTitle:(NSString *)title {
    GGToastView *toastView = [self createToastViewWithTile:title];
    [toastView startLoading];
    [[[UIApplication sharedApplication] keyWindow] addSubview:toastView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(DISMISS_TIME * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [GGToastView dissmissLoadingView];
        
    });
}


+ (GGToastView *)createToastViewWithTile:(NSString *)title {
    
    GGToastView *toastView = [GGToastView sharedToastView];
    
    toastView.frame = CGRectMake(DEFAULT_X, DEFAULT_Y, TOAST_WDITH, TOAST_HEIGHT);
    
    toastView.layer.cornerRadius = CORNERRADIUS;
    
    toastView.layer.masksToBounds = YES;
    
    toastView.tipsLabel.text = title;
    
    [toastView.tipsLabel sizeToFit];
   
    toastView.tipsLabel.frame = [toastView tipsLabelFrame];
    
    toastView.indecator.frame = [toastView indecatorFrame];
   
    return toastView;
}


- (CGRect)tipsLabelFrame {
    CGFloat tipsLabelX = (TOAST_WDITH - self.tipsLabel.bounds.size.width) / 2;
    CGFloat tipsLabelY = (TOAST_HEIGHT - self.tipsLabel.bounds.size.height) / 2;
    return CGRectMake(tipsLabelX, tipsLabelY, self.tipsLabel.bounds.size.width, self.tipsLabel.bounds.size.height);
}

- (CGRect)indecatorFrame {
    
    return CGRectMake(CGRectGetMinX(self.tipsLabel.frame) - TOAST_HEIGHT, 0, TOAST_HEIGHT, TOAST_HEIGHT);
}

- (void)startLoading {
    [self.indecator startAnimating];
}

- (void)stopLoading {
    [self.indecator stopAnimating];
}

- (void)setupUI {
    
    [self addSubview:self.tipsLabel];
    [self addSubview:self.indecator];
}

- (UIActivityIndicatorView *)indecator {
    if (_indecator == nil) {
        _indecator = [[UIActivityIndicatorView alloc] init];
        _indecator.hidden = YES;
    }
    return _indecator;
}


- (UILabel *)tipsLabel {
    
    if (_tipsLabel == nil) {
        
        _tipsLabel = [[UILabel alloc] init];
        
        _tipsLabel.font = [UIFont systemFontOfSize:15];
        
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        
        _tipsLabel.textColor = [UIColor whiteColor];
        
        _tipsLabel.layer.cornerRadius = CORNERRADIUS;
        
        _tipsLabel.layer.masksToBounds = YES;
        
        _tipsLabel.text = @"GGTost";
        
        
    }
    
    return _tipsLabel;
}





@end
